package cn.donting.jme.swing.tab.scene.treenode;

import cn.donting.jme.EditorContent;
import cn.donting.jme.editor.scene.Scene;
import cn.donting.jme.editor.scene.SceneCamera;
import cn.donting.jme.event.SelectEvent;
import cn.donting.jme.event.camera.SelectCameraEvent;
import cn.donting.jme.swing.tab.scene.SceneTreeTab;
import cn.donting.jme.utile.IconUtil;
import lombok.Getter;

import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

@Getter
public class CameraTreeNode extends SceneTreeNode {
    private SceneCamera sceneCamera;
    private JTree jTree;
    private Scene scene;
    private JPopupMenu jPopupMenu;

    public CameraTreeNode(JTree jTree, Scene scene, SceneCamera sceneCamera) {
        this.jTree = jTree;
        this.sceneCamera = sceneCamera;
        this.scene = scene;
        EditorContent.setObjectData(sceneCamera, SceneTreeTab.TREE_NODE_DATA_KEY, this);
    }

    @Override
    public String toString() {
        return sceneCamera != null ? sceneCamera.getCamera().getName() : "ROOT-Camera";
    }

    public void openMenu(MouseEvent mouseEvent) {
        if (jPopupMenu != null) {
            jPopupMenu.show(jTree, mouseEvent.getX(), mouseEvent.getY());
            return;
        }
        jPopupMenu = new JPopupMenu();
        JMenuItem refresh = new JMenuItem("刷新");
        refresh.setEnabled(sceneCamera == null);
        refresh.setIcon(IconUtil.getIcon("icon/refresh.svg"));
        refresh.addActionListener(this::menuRefreshActionEvent);
        jPopupMenu.add(refresh);
        jPopupMenu.show(jTree, mouseEvent.getX(), mouseEvent.getY());
    }

    @Override
    public SelectEvent getSelectEvent() {
        if(sceneCamera==null){
            return null;
        }
        return new SelectCameraEvent(jTree,sceneCamera);
    }

    private DefaultTreeModel getTreeModel() {
        return (DefaultTreeModel) jTree.getModel();
    }

    private void menuRefreshActionEvent(ActionEvent e) {
        removeAllChildren();
        ArrayList<SceneCamera> cameras = scene.getCameras();
        for (SceneCamera camera : cameras) {
            add(new CameraTreeNode(jTree, scene, camera));
        }
        getTreeModel().nodeStructureChanged(this);
    }

    public static CameraTreeNode createTreeNode(JTree jTree, Scene scene) {
        CameraTreeNode cameraTreeNode = new CameraTreeNode(jTree, scene, null);
        ArrayList<SceneCamera> cameras = scene.getCameras();
        for (SceneCamera camera : cameras) {
            cameraTreeNode.add(new CameraTreeNode(jTree, scene, camera));
        }
        return cameraTreeNode;
    }

    @Override
    public Icon getIcon(int size) {
        if (sceneCamera == null) {
            return IconUtil.getIcon("icon/cameras.svg", size);
        } else {
            return IconUtil.getIcon("icon/camera.svg", size);
        }
    }

    @Override
    public String getName() {
        if (sceneCamera == null) {
            return "相机列表";
        } else {
            String objectId = EditorContent.getObjectId(sceneCamera);
            String name = sceneCamera.getCamera().getName();
            if (name==null) {
                return "-"+objectId;
            }
            return sceneCamera.getCamera().getName() + objectId;
        }
    }
}